{% extends "base.html" %}
{% block title %}{% endblock %}
{% block content %}
<style type="text/css">
    	body {background:#f1efef;}
		.layui-input{height:35px;width:85%; padding-left: 5px;font-size:16px;display:inline-block;}
		.layui-btn {height:35px;}
		.layui-form {width:30%;height:225px;margin:0 auto;margin-top:12%;padding:15px 28px 0px;background:#fff;}
		.layui-input-block {margin-left:0;}
		.layui-input-block a {color:blue;float:right;line-height:30px;font-size:14px;}
		h1 {text-align:center;color:#1d598e;}
    </style>

<form class="layui-form" action="" method="post">
		<div class="layui-form-item">
			<h1>山城测试平台登录</h1>
		</div>
		<div class="layui-form-item">
		    <div class="layui-input-block">
				<span class="decrib">账号：</span>
				<input type="text" name="userName" id= "userName"  placeholder="请输入账号" autocomplete="off" class="layui-input" autofocus="true" required>
		    </div>
		</div>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<span class="decrib">密码：</span>
				<input type="password" name="passWord"  id= "passWord"   placeholder="请输入密码" autocomplete="off" class="layui-input" required>
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn layui-btn-fluid"   lay-submit lay-filter="start" type="submit">登 录</button>
			</div>
		</div>
	</form>
<script>
    layui.use(['table','code'], function(){
          var table = layui.table
          ,form = layui.form;
          var $= layui.jquery;
          //从缓存中读取用户名和密码
          UserInfo =  layui.data('test').UserInfo;
          if(UserInfo!=undefined){
             $("#userName").val(UserInfo.userName);
             $("#passWord").val(UserInfo.passWord);
          }

          form.on('submit(start)', function(data){
            //发送登录请求
            userName = $("#userName").val();
            passWord = $("#passWord").val();
            layui.data('test',{key:"UserName",value:userName});
            $.ajax({
                        //发出请求
                        type:"POST",
                        url:"/login",
                        contentType: 'application/json; charset=utf-8',
                        dataType:"json",
                        async:false,
                        data:JSON.stringify(data.field),
                        success: function(result){
                            if(result.data!=null&&result.data!=undefined){
                                //将用户名、密码、角色、openId、accessToken存储起来
                                layui.data('test', {key: 'UserInfo',value: {"userId":result.data.id,"passWord":passWord,"userName":userName,"openId":result.data.openId,"role":result.data.role,"token":result.data.token}});

								var winWidth = $(window).width() - 250;
  								var winHeight = $(window).height();
                                window.location.href="/index?token="+result.data.token+"&openId="+result.data.openId+"&winWidth="+winWidth+"&winHeight="+winHeight;
                            }else{
                                layer.msg("用户名或者密码错误!");
                            }
                        }
            });
            return false;

          });

    });
</script>
{% endblock %}